package cc.bluemc.modules.adminUser.action;

import cc.bluemc.modules.adminUser.entity.Permission;
import cc.bluemc.modules.adminUser.service.PermissionService;
import cc.bluemc.platform.comm.BaseAction;
import cc.bluemc.platform.jdbctemplatetool.exception.NoColumnAnnotationFoundException;
import cc.bluemc.platform.jdbctemplatetool.exception.NoIdAnnotationFoundException;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin/permission")
public class PermissionAction extends BaseAction{
	@Autowired
	private PermissionService permissionService;
	
	/**
	 * 查看所有功能
	 * @param model
	 * @return
	 * @throws NoIdAnnotationFoundException
	 * @throws NoColumnAnnotationFoundException
	 */
	@RequestMapping("view")
	public String view(Model model) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		List<Permission> permiss = permissionService.getAll();
		model.addAttribute("permissionTree",JSON.toJSONString(permiss));
		return "adminUser/permissionView";
	}
	@RequestMapping("form")
	public String form(Integer id,Model model) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		if(null!=id){
			Permission permi = permissionService.get(id);
			model.addAttribute("bean", permi);
		}
		List<Permission> permiss = permissionService.getAll();
		model.addAttribute("permissionTree",JSON.toJSONString(permiss));
		return "adminUser/permissionForm";
//		 return "redirect:view";
	}
	@RequestMapping("save")
	public String save(Permission bean,Model model) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		try {
			permissionService.save(bean);
			resultMap.setSuccess(true);
			
		} catch (Exception e) {
			resultMap.setSuccess(false);
			resultMap.setMsg("保存失败");
		}
		return "redirect:view";
	}
}
